<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }

        #wrap {/*大盒子样式*/
            width: 400px;
            margin: 200px auto;
            border: 1px solid red;
            user-select: none;
        }

        #wrap ul {
            display: flex;
        }
        #wrap li {
            margin: 0 3px;
        }

        #wrap li:not(.dot) { /*数字盒子样式*/
            width: 100%;
            height: 62px;
            background: linear-gradient(to bottom, #434343 50%, #393939 50%);
            border-radius: 10px;
            line-height: 62px;
            text-align: center;
            color: #fff;
            font-size: 26px;
        }

        #wrap .dot { /*点盒子的样式*/
            width: 20px;
            height: 62px;
            line-height: 62px;
            text-align: center;
            font-size: 26px;
            font-weight: bolder;
        }
    </style>
</head>
<body>
<div id = "wrap">
    <ul>
        <li class = "hours">0</li>
        <li class = "hours">0</li>
        <li class = "dot">:</li>
        <li class = "minutes">0</li>
        <li class = "minutes">0</li>
        <li class = "dot">:</li>
        <li class = "seconds">0</li>
        <li class = "seconds">0</li>
    </ul>
</div>

<script>
    let li = document.querySelectorAll("#wrap li:not(.dot)") //反选，选中除了类名为.dot的li

    //先获取到当前的时间 在定时器中使用window.onload就可以立马刷新时间
    setInterval(window.onload = function (){

        //获取当前时间
        let date = new Date()

        //获取时分秒
        let h = date.getHours() //时
        let m = date.getMinutes() //分
        let s = date.getSeconds() //秒

        //将获取的时分秒分别传入补零函数进行格式统一并重新赋值
        h = toTwo(h)
        m = toTwo(m)
        s = toTwo(s)

        //将格式统一后的数值放入一个字符串中
        let clockStr = h + m + s

        //用一个for循环将字符串每一位数值与li标签每一位一一对应
        for(let i = 0;i < clockStr.length;i++){
            li[i].innerHTML = clockStr[i] //li列表中每一位对应clockStr中的每一位
        }

    },1000) //每隔1秒执行一次获取当前时间

    //各位补零函数
    function toTwo(number){ //接收一个参数数字
        if(number < 10){
            return "0" + number
        }else{
            return "" + number
        }
        //如果用三目运算符表示上面条件判断代码
        //return (number < 10 ? "0" : "") + number
    }


</script>
</body>
</html>